package com.qf.mvc2202.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;

@Controller
@RequestMapping("/pick")
public class PickNameController {
    @RequestMapping("/test1")
    public String test1(HttpSession session, HttpServletRequest req) throws IOException {
        ArrayList<String> list=new ArrayList();
        InputStream inputStream = getClass().getResourceAsStream("/names.txt");
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8");
        BufferedReader bf=new BufferedReader(inputStreamReader);
        String line=null;
        while ((line= bf.readLine())!=null){
            list.add(line);
        }
        Random random = new Random();
        int i=random.nextInt(list.size());
        String s = list.get(i);
        System.out.println(s);
        req.setAttribute("name",s);
        bf.close();
        return "forward:/views/result.jsp";
    }
}
